package com.lzy.handle.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lyw
 * @Date 2023/2/1-下午2:46
 */
@Component
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
  @Override
  public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
    Map<String,Object> result = new HashMap<>();
    result.put("msg", "登录成功！");
    result.put("status", 200);
    result.put("authentication", authentication);
    response.setContentType("application/json;charset=UTF-8");
    String string = new ObjectMapper().writeValueAsString(result);
    response.getWriter().println(string);
  }
}
